package com.metagurukul.informer.dao;

import com.metagurukul.informer.utility.ConnectionFactory;
import com.metagurukul.informer.valueObjects.*;
import java.sql.*;

/**
 * Class UsersDAO for interaction with the users table of database
 * @author 
 *
 */
public class UsersDAO
{	
	/**
	 * ...Method for finding a user by its id
	 * @param id
	 * @return user
	 */
    public Users findById(String id) throws SQLException{
	   	Users user=null;
	   	Connection	con= ConnectionFactory.getInstance();
	   		
	   	 /*
	   	  * SQL query for extracting the users row from the table
	   	  */
	   	 String sql = "SELECT * FROM users WHERE id='"+id+"'";
		 Statement stm = con.createStatement();
		 ResultSet rs=stm.executeQuery(sql);
		 /*
		  * making an user object and initializing it by the query output 
		  */
		 if(rs.next()){
		 	user=new Users(rs.getString("id"),rs.getString("name"),rs.getString("password"));
		 }
	   
		 if(stm!= null){
		   	stm.close();
	    }
	    if(rs!= null){
	    	rs.close();
	    }
	    if(con!= null){
	    	con.close();
	    }
		 
		return user;	 
	   }
}